package zhtt.hibernate.user.controller;

import com.gitee.zhtt.entity.JsonResponse;
import com.gitee.zhtt.third.json.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zhtt.hibernate.user.entity.User;
import zhtt.hibernate.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
   
    @Autowired
    private UserService userService;
   
    @RequestMapping("/save")
    public JsonResponse save(@RequestBody User userInfo){
        userInfo = userService.save(userInfo);
        System.out.println("controler.save:"+ JsonUtils.toString(userInfo));
       return JsonResponse.success(userInfo);
    }

    @RequestMapping("/saves")
    public JsonResponse save1(){
        List<User> userList = new ArrayList<User>();
        for(int i=0;i<2;i++){
            User userInfo = new User();
            userInfo.setName("name-"+i);
            userInfo.setAge(20+i);
            userService.save(userInfo);
            userList.add(userInfo);
        }
       return JsonResponse.success(userList);
    }
   
    @RequestMapping("/query")
    public JsonResponse query(@RequestParam("pageSize")int pageSize){
       return JsonResponse.success(pageSize);
    }

    @RequestMapping("/find")
    public JsonResponse find(){
        List<User> userList = (List<User>) userService.findAll();
       return JsonResponse.success(userList);
    }
   
    @RequestMapping("/findByName")
    public JsonResponse findByName(){
        List<User> userList = (List<User>) userService.findByName("1");
        return JsonResponse.success(userList);
    }
}
